package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.system.domain.HUAWEI.EntityRevisionDTO;
import com.ruoyi.system.domain.HUAWEI.Software;
import com.ruoyi.system.domain.function.ProdOrder;
import com.ruoyi.system.domain.function.ProdPlan;
import com.ruoyi.system.domain.prod.ProdPsInfoVO;
import com.ruoyi.system.domain.prod.QueryProdPsInfoDTO;
import com.ruoyi.system.mapper.BaseStationMaterialMapper;
import com.ruoyi.system.mapper.HUAWEI.SoftwareMapper;
import com.ruoyi.system.mapper.ProdPlanMapper;
import com.ruoyi.system.service.ITBtpMtlCurriculumViateTiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.mapper.ProdOrderMapper;
import com.ruoyi.system.service.IProdOrderService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 工单信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-13
 */
@Service
public class ProdOrderServiceImpl extends ServiceImpl<ProdOrderMapper, ProdOrder> implements IProdOrderService {
    @Autowired
    private ProdPlanMapper prodPlanMapper;
    @Autowired
    private ITBtpMtlCurriculumViateTiService tbService;
    @Autowired
    private SoftwareMapper softwareMapper;
    @Autowired
    private BaseStationMaterialMapper baseStationMaterialMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Long> asList) {
        if (IterUtil.isNotEmpty(asList)) {
            //删除成功回滚生产计划下发数量与剩余下发数量
            asList.stream().forEach(i -> {
                ProdOrder prodOrder = baseMapper.selectById(i);
                ProdPlan prodPlan = prodPlanMapper.selectOne(new QueryWrapper<ProdPlan>().eq("plan_code", prodOrder.getPlanCode()));
                prodPlan.setComNumber(prodPlan.getComNumber() - prodOrder.getOrderNumber());
                prodPlan.setSurplusNumber(prodPlan.getSurplusNumber() + prodOrder.getOrderNumber());
                prodPlanMapper.update(prodPlan, new QueryWrapper<ProdPlan>().eq("plan_code", prodOrder.getPlanCode()));
                baseMapper.deleteById(i);
            });
        }
        return true;
    }

    @Override
    public AjaxResult getPoVersion(String po,Long orderId) {
        List<Software> sos = softwareMapper.selectList(new QueryWrapper<Software>()
                .eq("entity_name", po));
        return AjaxResult.success(sos);
    }

    @Override
    public AjaxResult updatePoVersion(Software software) {
        if(ObjectUtil.isNull(software)){
            throw new CustomException("修改数据不能为空");
        }
        softwareMapper.updateById(software);
        return AjaxResult.success();
    }

    @Override
    public List<ProdPsInfoVO> psInfo(QueryProdPsInfoDTO dto) {
        List<ProdPsInfoVO> list = baseMapper.selectPsInfo(dto);
        //根据物料编码和产线编码获取工位编码
        if(IterUtil.isNotEmpty(list)){
            list.stream().forEach(i->{
                List<String> stationCodes = baseStationMaterialMapper.getGwCode(i.getMaterielid(),i.getLineCode());
                if(IterUtil.isNotEmpty(stationCodes)){
                    i.setRoutingCode(stationCodes.get(0));
                }
            });
        }
        return list;
    }

}
